<?

/******************************************************************************
 ***
 *** PHP Sub Class Document
 *** Name:	System::Calendar.inc
 *** Owner:	eMobile Communications, LLC (Enrique A. Reyes-Otero)
 *** Developer:	Enrique A. Reyes-Otero
 *** Support:	support@emobilecommunications.com
 *** Copyright:	2007 © PHP System API
 *** Licensing: This document is governed by the eMobile Communications System
 ***		License found at in the System directory parallel to
 ***		this document.
 *** Legal:	eMobileCommunications.COM is the prorietor owner of this
 ***		document and the contents there in. If you have not been
 ***		authorized access to this document or its functionality then
 ***		your are advised to remove this document from your possesion.
 ***		Modification of this document without eMobileCommunications.COM
 ***		legal consent or any illegal or misuse of this document or the
 ***		contents and the functionality therein shall be prosecuted to
 ***		the full extent of the law.
 ***
 ******************************************************************************/

class Calendar {

	var $wkdys = array (
		array ("Sunday", "Sun"),
		array ("Monday", "Mon"),
		array ("Tuesday", "Tue"),
		array ("Wednesday", "Wed"),
		array ("Thursday", "Thu"),
		array ("Friday", "Fri"),
		array ("Saturday", "Sat"),
	);

	var $debug = true;
	var $week;

	function Calendar ($options) {

		$this->date = localtime (($options && $options["timestamp"]) ?
				$options["timestamp"] : mktime ());
		$this->local_date = $this->date;
		$this->months = $this->get_months ();

		$this->today = mktime ("00", "00", "00",
				$this->local_date[4] + 1,
				$this->local_date[3],
				$this->local_date[5]+1900);
		$this->current_month = $this->local_date[4];
	}

	function get_weekdays () {

		return $this->wkdys;
	}

	function create_week ($current = 0) {

		$week = new Week ();

		$date = localtime($this->today + (60 * 60 * 24 * 7 * $current));
		$week->begin = $this->today + (60 * 60 * 24 * 7 * $current)
				- ($date[6] * 60 * 60 * 24);
		$week->end = $week->begin + (60 * 60 * 24 * 7);

		for ($i < 0; $i < 7; $i++) {

			$day = new Day ();
			$d = localtime ($week->begin + (60 * 60 * 24 * $i));
			$day->sdy = $d[3];
			$day->smo = $d[4] + 1;
			$day->syr = $d[5] + 1900;
			$day->set_hours ();

			$day->begin = mktime ("00","00","00",
					$day->smo,$day->sdy,$day->syr);

			$d = localtime ($week->begin + (60 * 60 * 24 * $i) +
					(60 * 60 * 24));

			$day->edy = $d[3];
			$day->emo = $d[4] + 1;
			$day->eyr = $d[5] + 1900;
			$day->end = mktime ("00","00","00",
					$day->emo,$day->edy,$day->eyr);

			$dbegin = localtime ($day->begin);
			if ($this->current_month == $dbegin[4])
					$day->cmo = true;
			array_push ($week->days, $day);
		}

		$this->week = $week;

		return $week;
	}

	function get_timestamp ($date) {

		$time = localtime ($date);

		return sprintf ("%s-%s-%s %s:%s:%s",
				$time[5]+1900,
				$time[4]+1<10?"0".($time[4]+1):$time[4]+1,
				$time[3]<10?"0".$time[3]:$time[3],
				$time[0]<10?"0".$time[0]:$time[0],
				$time[1]<10?"0".$time[1]:$time[1],
				$time[2]<10?"0".$time[2]:$time[2]);
			
	}

	function get_time ($date = 0, $notation = false) {

		$time = localtime ($date ? $date : $this->today);

		if ($notation && $time[2] > 12) $value = "PM";
		elseif ($notation) $value = "AM";

		$time[2] = ($notation && $time[2] > 12) ?
				$time[2] - 12 : $time[2];
		$value = sprintf ("%s:%s:%s",
				$time[2]<10?"0".$time[2]:$time[2],
				$time[1]<10?"0".$time[1]:$time[1],
				$time[0]<10?"0".$time[0]:$time[0]) . $value;
		return $value;
		
	}

	function get_date ($date = 0) {

		$time = localtime ($date ? $date : $this->today);

		return sprintf ("%s %s %d, %d",
				$this->wkdys[$time[6]][0],
				$this->months[$time[4]][2],
				$time[3],$time[5]+1900);
	}

	function sum_days ($current) {

		$this->current = $current;
		$today = localtime ($this->today);
		$today[4] += 1;

		$yr = $today[5] + 1900;

		$end = $i = 0;

		if ($current > 0) {

			for ($i = 0; $end != $current; $i++) {

				$date = localtime (mktime ("00", "00", "00",
						$today[4] + $i , "01", $yr));
				$this->months = $this->get_months ($date);
				#$mo = $date[4] + 2 > 11 ?
				$mo = $date[4] + 1 + $today[4] > 11 ?
						0 : $date[4] + 2;
				if ($today[3] > $this->months[$mo][1])
					$today[3] = $this->months[$mo][1];
				if ($today[4] + $i > 10) { $yr ++; $i -= 12; }

				$end++;
			}

			$i = $today[4] + $i;
		} elseif ($current < 0) {

			for ($i = 0; $end != $current; $i--) {

				$date = localtime (mktime ("00", "00", "00",
						$today[4] + $i, "01", $yr));
				$this->months = $this->get_months ($date);
				#$mo = $date[4] - 2 < 0 ?
				$mo = $date[4] - 1 - $today[4] < 0 ?
						11 : $date[4] - 2;
				if ($today[3] > $this->months[$mo][1])
					$today[3] = $this->months[$mo][1];
				if ($today[4] - $i < 1) { $yr--; $i -= 12; }

				$end--;
			}

			$i = $today[4] + $i;
		}

		return localtime ($this->today = mktime ("00", "00", "00",
				$i, $today[3], $yr));
	}

	function create_month ($current = 0) {

		$month = new Month ($current);

		if ($current) {

			$date = $this->sum_days ($current);
			$this->date = $this->local_date = $date;
		} else {

			$this->date = $this->local_date;
		}

		$this->current_month = $this->date[4];

		$first = mktime ("00", "00", "00",
				$this->date[4] + 1, "01",
				$this->date[5] + 1900);

		$wk = $this->create_week ();

		if (!$current) $wk->current = true;

		$i = 0;

		array_push ($month->weeks, $wk);
		$current = $wk->begin;

		while ($current > $first) {

			$swk = $this->create_week (--$i);
			array_unshift ($month->weeks, $swk);
			$current = $swk->begin;
		}

		$current = 5 + $i;

		for ($i = 0; $i < $current; $i++ ) {

			$swk = $this->create_week ($i + 1);
			array_push ($month->weeks, $swk);
		}

		$this->month = $month;

		return $month;
	}

	function get_months ($mos = array ()) {

		if (!count ($mos)) $mos = $this->local_date;
		return array (
			array ("Jan","31","January"),
			array ("Feb",($mos[5]+1900)%4?28:29,"February"),
			array ("Mar","31","March"),
			array ("Apr","30","April"),
			array ("May","31","May"),
			array ("Jun","30","June"),
			array ("Jul","31","July"),
			array ("Aug","31","August"),
			array ("Sep","30","September"),
			array ("Oct","31","October"),
			array ("Nov","30","November"),
			array ("Dec","31","December"),
		);
	}

	function get_month () {

		return $this->months[$this->local_date[4]][2];
	}

	function get_year () {

		return $this->local_date[5] + 1900;
	}
}

class Month {
	
	var $weeks = array ();
	var $week = 0;
	var $current;

	function Month ($current) { $this->current = $current; }
	function iterate_weeks () {

		if ($this->week > 5) return $this->week = 0;
		return $this->weeks[$this->week++];
	}
}

class Day  {

	function get_day () {

		$day = localtime ($this->begin);
		return $day[3];
	}

	function get_week_day () {

		$day = localtime ($this->begin);
		return $day[6]+1;
	}

	function set_hours () {

		for ($i = 0; $i < 24; $i++) {

		}
	}
}

class Week {

	var $days = array ();
	var $day = 0;
	var $current;

	function Week ($current = 0) { $this->current = $current; }
	function iterate_days () {

		if ($this->day > 6) return $this->day = 0;
		return $this->days[$this->day++];
	}
}

?>
